import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/ex/kq_ex.dart';
import 'package:kq_flutter_core_widget/utils/toast_util.dart';
import 'package:kq_flutter_tv_widgets/widgets/formItem/entity/kq_form_entity.dart';
import 'package:kq_flutter_tv_widgets/widgets/formItem/kq_header_item_view.dart';
import 'package:kq_flutter_tv_widgets/widgets/gridView/kq_check_selectable_grid_view.dart';
import 'package:kq_flutter_tv_widgets/widgets/gridView/kq_selectable_grid_view.dart';

class KqSelectableGridViewDemo extends StatefulWidget {
  const KqSelectableGridViewDemo({super.key});

  @override
  State<StatefulWidget> createState() => KqSelectableGridViewDemoState();
}

class KqSelectableGridViewDemoState extends State<KqSelectableGridViewDemo> {
  List<IGridViewItemData> test = [
    IGridViewItemData("title1", isSelected: false, isUsed: false),
    IGridViewItemData("title2", isSelected: false, isUsed: false),
    IGridViewItemData(
        "很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长很长",
        isSelected: false,
        isUsed: false),
    IGridViewItemData("title5", isSelected: false, isUsed: true),
    IGridViewItemData("title6", isSelected: false, isUsed: false),
  ];

  List<ICheckGridViewItemData> test3 = [
    ICheckGridViewItemData("title1", gridViewItemIsSelected: true),
    ICheckGridViewItemData("title2", gridViewItemIsSelected: false),
    ICheckGridViewItemData("title3", gridViewItemIsSelected: false),
    ICheckGridViewItemData("很长很长很长很长很长很长很长很长很长很长很长很长很长很长", gridViewItemIsSelected: true),
    ICheckGridViewItemData("title5", gridViewItemIsSelected: true),
    ICheckGridViewItemData("title6", gridViewItemIsSelected: true),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            KqHeaderItemView(
              entity: KqFormEntity(
                title: "可选择的KqSelectableGridView演示：",
              ),
            ),
            KqSelectableGridView(
              items: test,
              onTap: (data) {
                if (data != null) {
                  KqToast.showNormal(data.title);
                }
              },
            ),
            KqHeaderItemView(
              entity: KqFormEntity(
                title: "一体机KqCheckSelectionGridView演示：",
              ),
            ),
            KqCheckSelectableGridView(
              items: test3,
              multiSelect: true,
              onMultiTap: (sourceData,data) {
                if (data.isNotNullOrEmpty) {
                  KqToast.showNormal(concatDynamic(data, (t) => t.gridViewItemTitle));
                }
              },
            ),
          ],
        ),
      ),
    );
  }

  static String? concatDynamic<T>(
      List<T>? list, String? Function(T t) concatConverter,
      {String split = ',',
      String? defaultValue,
      bool concatWhenEmpty = false}) {
    if (list == null) {
      return defaultValue;
    }
    String str = '';
    for (var value in list) {
      if (str.isEmpty) {
        str = concatConverter(value) ?? '';
      } else {
        str = concatString(str, concatConverter(value), split,
            concatWhenEmpty: concatWhenEmpty);
      }
    }
    return str.isEmpty ? defaultValue : str;
  }

  static String concatString(String? str1, String? str2, String split,
      {bool concatWhenEmpty = false}) {
    if (str1.isNullOrEmpty) {
      if (concatWhenEmpty) {
        return '$split${str2 ?? ''}';
      } else {
        return str2 ?? '';
      }
    }
    if (str2.isNullOrEmpty) {
      if (concatWhenEmpty) {
        return '$str1$split';
      } else {
        return str1!;
      }
    }
    return '$str1$split$str2';
  }
}
